package com.ming.service.impl;

import com.ming.dao.BookDao;
import com.ming.domain.Book;
import com.ming.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

//    缓存
    private HashMap<Integer,Book> cache = new HashMap<>();
    @Override
    public boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }


    @Override
    public Book getById(Integer id) {
        Book book = cache.get(id);
        if(book==null){
            Book queryBook = bookDao.selectById(id);
            cache.put(id,queryBook);
            return queryBook;
        }else {
            Book bookCache = cache.get(id);
            return bookCache;
        }
    }

    @Override
    @Cacheable(value = "cacheSpace",key = "#id")
    public Book getById2(Integer id) {
        Book queryBook = bookDao.selectById(id);
        return queryBook;
    }
}
